package org.lc.stk.model.course;

import java.util.List;

import org.hibernate.annotations.Comment;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;

/**
 * 章节，默认每门课程都一个root章节，root章节名称与课程同名，上级为null，子章节默认排序为1
 */
@Entity
@Table(name = "chapter")
@Comment("章节")
public class Chapter {

public enum ChapterState {
    ACTIVE(1),
    INACTIVE(2);
    private final int value;
    ChapterState(int value) {
        this.value = value;
    }
    public int getValue() {
        return value;
    }
}

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

    @Column
    @Comment("章节名称")
    private String name;

    /**
     * 上级章节
     */
    @ManyToOne
    @Comment("上级章节")
    private Chapter parent;

    @OneToMany(mappedBy="parent", cascade={jakarta.persistence.CascadeType.ALL}, orphanRemoval=true)
    private List<Chapter> children;

    @Column
    @Comment("章节顺序")
    private Integer sort;
    
    @Column(columnDefinition = "int default 1")
    @Comment("状态，1 在用，2 停用或删除")
    private Integer state;
    
    @ManyToOne
    @Comment("课程")
    private Course course;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Chapter getParent() {
        return parent;
    }

    public void setParent(Chapter parent) {
        this.parent = parent;
    }

    public Course getCourse() {
        return course;
    }

public void setCourse(Course course) {
    this.course = course;
}   
    
public void setState(ChapterState state) {
    this.state = state.getValue();
}
    
public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public List<Chapter> getChildren() {
        return children;
    }

    public void setChildren(List<Chapter> children) {
        this.children = children;
    }
    @Override
    public String toString() {
        return "Chapter [id=" + id + ", name=" + name + "]";
    }

}
